<template>
  <div class="searchList">
    <div class="list-top">
      <!-- 商品列表 -->
      <van-nav-bar title="商品列表" left-arrow @click-left="$router.go(-1)"/>

      <!-- 搜索框 -->
      <van-search show-action placeholder="请输入搜索关键词" :value="querySearch || '搜索商品'">
        <template #action>
          <div><van-icon name="apps-o" size="20" color="#1989fa"/></div>
        </template>
      </van-search>
    </div>

    <!-- 排序筛选 -->
    <div class="sort-btns">
      <div class="sort-item">综合</div>
      <div class="sort-item">销量</div>
      <div class="sort-item">价格</div>
    </div>

    <!-- 商品列表 -->
    <div class="goods-list">
      <GoodsItem v-for="item in proList" :key="item.goods_id" :item="item"></GoodsItem>
    </div>
  </div>
</template>

<script>
import GoodsItem from '@/components/GoodsItem'
import { getProList } from '@/api/product'
export default {
  name: 'searchList',
  data () {
    return {
      page: 1, // 默认返回第一页
      proList: [] // 存储商品列表
    }
  },
  components: {
    GoodsItem
  },
  computed: {
    // 获取地址栏的搜索关键字
    querySearch () {
      return this.$route.query.search
    }
  },
  async created () {
    const { data: { list } } = await getProList({
      // categoryId 是从商品分类页面跳转过来的搜索
      categoryId: this.$route.query.categoryId,
      goodsName: this.querySearch,
      page: this.page
    })
    console.log(list)
    this.proList = list.data
  }
}
</script>

<style lang="less" scoped>
.searchList{
  .van-search__action{
    margin-right: 10px;
    align-content: center;
  }
  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
  // 商品样式
  .goods-list {
    background-color: #f6f6f6;
  }
}
</style>
